import Vue from 'vue';
import Request from "@/sdk/pocky-request/index.js";
var vm = new Vue();
/**
 * 用户模块store
 */
const user = {
	// 状态
	state: {
		isLogin: false,
		userInfo: null,
		token: null,
		parentId:0,
		openid:null,
		imid:null
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		isLogin: (state, payload) => {
			state.isLogin = payload
		},
		openid: (state, payload) => {
			state.openid = payload
		},
		userInfo: (state, payload) => {
			state.userInfo = payload
		},
		parentId: (state, payload) => {
			state.parentId = payload
		},
		imid: (state, payload) => {
			state.imid = payload
		},
		token: (state, payload) => {
			state.token = payload;
			uni.setStorageSync('token', payload)
		},
		mobile: (state, payload) => {
			
			state.mobile = payload
			uni.setStorageSync('mobile', payload)
		}
	},
	// 异步方法 dispatch
	actions: {
		
		/**
		 * @description  自动登录
		 * @param {Object} context
		 * @param {Object} _data:{token:'xxxxx'}
		 */
		apiUserAutoLogin(context, _data) {
			return new Promise((resolve, reject) => {
				
				Request().request({
					url: 'api/wxapi/pd_token',
					method: 'GET',
					data: _data
				}).then(res => {					
					if (res.data.code == 1) {						
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("openid",res.data.data.openid);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '您的登录信息已过期,请重新登录',
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},		
		/**
		 * @description 微信登录接口
		 * @param {Object} context
		 * @param {Object} _data 
		 */	
		apiThirdLogin(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxapi/login',
					method: 'GET',
					data: _data
				}).then(res => {					
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("openid",res.data.data.openid);
						this.commit("isLogin", true);
						uni.setStorageSync("userInfo",res.data.data)
						resolve(res)
					} else {
						reject(res)
					}
		
				}).catch(err => {
					reject(res)
				})
			})
		},
		
		getimid(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxim/get_imid',
					method: 'GET',
					data: _data
				}).then(res => {
					console.log(res)
					if (res.data.code == 1) {													
						resolve(res)
					} else {
						reject(res)
					}
		
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context  获取微信code
		 */
		loginByWexin(context) {
			return new Promise((resolve, reject) => {
				wx.login({
					success: (res) => {
						if (res.code) {			//微信登录成功 已拿到code
							resolve(res.code)						
						} else {
							reject(res)
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			})
		
		},
		// getgzhcode(context, _data) {
		// 	return new Promise((resolve, reject) => {
		// 		Request().request({
		// 			url: '/api/Wxsendmsg/GET_Code',
		// 			method: 'GET',
		// 			data: _data
		// 		}).then(res => {															
		// 				reject(res)															
		// 		}).catch(err => {
		// 			reject(res)
		// 		})
		// 	})
		// },
		/**
		 * @param {Object} context 保存手机号
		 */		
		getphonenum(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxapi/save_phone',
					method: 'GET',
					data: _data
				}).then(res => {
					
					if (res.data.code == 1) {						
						this.commit("mobile",res.data.data.mobile);						
						resolve(res)
					} else {
						reject(res)
					}
					
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 获取首页公告
		 */
		getshouyegg(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxnews/index_newslist',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 学校公告
		 */
		schoollist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxnews/school_newslist',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 班级公告
		 */
		classlist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxnews/class_newslist',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 通告的详情
		 */
		getxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxnews/notice_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 身份列表
		 */
		getshenfenlist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxapi/get_students',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 首页数据展示
		 */
		showshouyeshujv(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxapi/switchover',
					method: 'GET',
					data: _data
				}).then(res => {					
					if(res.data.code==1 && res.data.data.user_im){
						this.commit("imid", res.data.data.user_im.imid);
					}
					resolve(res)
					
				}).catch(err => {
					reject(err)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 班级教师列表
		 */
		showteacherList(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxapi/class_teacher',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data食谱列表
		 */
		weekfoodlist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxcookbook/cook_list',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 食谱详情
		 */
		weekfoodxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxcookbook/cook_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 课表列表
		 */
		kebiaolist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxcookbook/kebiao_list',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 课表详情
		 */
		kebiaoxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxcookbook/kebiao_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data  通讯录列表
		 */
		tongxunlushow(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxim/im_list',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 收发邮件列表
		 */
		useremailxx(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/wxemail/email_list',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 获取邮件发送人信息
		 */
		useremaillist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/Wxemail/get_leaderlist',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 邮件详情
		 */
		maillistxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxemail/email_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 发送邮箱
		 */
		fasongyouxiang(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/Wxemail/send_email',
					method: 'post',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 发送班级公告
		 */
		fasongbanjigg(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxnews/send',
					method: 'post',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 问卷列表
		 */
		wenjuanlist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/SurveyApi/serList',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 问卷详情
		 */
		wenjuanxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/SurveyApi/index',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 问卷问题提交
		 */
		wenjuanqutijiao(context, _data) {  
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/SurveyApi/setReplay',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 家长助手列表
		 */
		jiazhangzhushoulist(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxmessage/index',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data家长助手详情
		 * 
		 */
		jiazhangxiangqing(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/api/Wxmessage/detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		
		/**
		 * @param {Object} context 上传接口
		 */
		uploadimages(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/send',
					method: 'POST',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 获取班级圈首页数据
		 */
		showbanjiquan(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/index',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 点赞接口
		 */
		showdainzan(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/classring_tags',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 发送评论
		 */
		fasongpinglun(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/classring_review',
					method: 'POST',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 获取评论列表
		 */
		showpinglunlist(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/review_index',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 删除班级圈
		 */
		deletebanjiquan(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/ring_del',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 删除评论
		 */
		deletepinglun(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/review_del',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data  家长助手点赞
		 */
		jiazhangshowdan(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxmessage/tags',
					method: 'POST',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 家长评论接口
		 */
		fajzsongpinglun(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxmessage/review',
					method: 'POST',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 教育局端详情接口
		 */
		jiaoyujvxq(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxcompany/edu_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 班级圈新消息列表接口
		 */
		newslistshow(context,_data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxclassring/notice_detail',
					method: 'GET',
					data: _data
				}).then(res => {
					
					resolve(res)
					
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 支付接口
		 */
		getpay(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'api/wxpay/pay',
					method: 'GET',
					data: _data
				}).then(res => {
					resolve(res)
		
				}).catch(err => {
					reject(res)
				})
			})
		},
		
	
		
		
		
		
		
	
	
	
		
		
		
		
		
		
		
		
		
	
	
	}
}

export default user
